package com.epfresh.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.PopupAddressEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Chen
 * Date: 2018/6/11
 * Desc:
 */
public class PopupAddressAdapter extends BaseAdapter {
    private int selectedPos = -1;
    private List<PopupAddressEntity> data = new ArrayList<>();

    public void setSelectedPos(int selectedPos) {
        this.selectedPos = selectedPos;
        notifyDataSetChanged();
    }

    public void setData(List<PopupAddressEntity> data) {
        if (this.data != null) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }
    }

    public List<PopupAddressEntity> getData() {
        return data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_choose_address, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.txtName = convertView.findViewById(R.id.list_item_address_name);
            viewHolder.imgChooseRight = convertView.findViewById(R.id.list_item_address_right);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txtName.setText(data.get(position).getName());
        if (selectedPos == position) {
            viewHolder.txtName.setTextColor(Color.parseColor("#24af75"));
            viewHolder.imgChooseRight.setVisibility(View.VISIBLE);
        } else {
            viewHolder.txtName.setTextColor(Color.parseColor("#313131"));
            viewHolder.imgChooseRight.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder {
        TextView txtName;
        ImageView imgChooseRight;
    }

}
